﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.DTO;
using System.Collections.Generic;
using HRProject.Code.Common;

namespace HRProject.Code.DAO
{
    public class JobTitleDAO : BaseDAO
    {
        public JobTitleDAO()
            : base ()
        {
        }

        public List<BLJobTitle> Search(BLJobTitle filter)
        {
            List<BLJobTitle> result = new List<BLJobTitle>();
            List<BaseCondition> condition = new List<BaseCondition>();
            condition.Add(new BaseCondition("@id", filter.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@name", filter.Name, ParameterDirection.Input));
            condition.Add(new BaseCondition("@privateCode", filter.PrivateCode, ParameterDirection.Input));
            DataTable data = select("usp_SearchJobTitles", condition);
            for (int i = 0; i < data.Rows.Count; i++)
            {
                BLJobTitle role = new BLJobTitle();
                role.Id = (Guid)data.Rows[i]["JobTitleId"];
                role.Name = (string)data.Rows[i]["JobTitleName"];
                role.Description = (string)data.Rows[i]["Description"];
                role.Avatar = (string)data.Rows[i]["Avatar"];
                role.DataSignature = (long)data.Rows[i]["DataSignature"];
                role.UpdateDate = (DateTime)data.Rows[i]["UpdateDate"];
                role.UpdateUserId = (Guid)data.Rows[i]["UpdateUserId"];
                role.Parent = new BLJobTitle();
                role.Parent.Id = (Guid)data.Rows[i]["ParentJobTitleId"];
                role.Parent.Name = string.Empty;
                result.Add(role);
            }
            return result;
        }

        public List<BLJobTitle> SearchByParentId(BLJobTitle filter)
        {
            List<BLJobTitle> result = new List<BLJobTitle>();
            List<BaseCondition> condition = new List<BaseCondition>();
            condition.Add(new BaseCondition("@parentId", filter.Parent.Id, ParameterDirection.Input));
            DataTable data = select("usp_SearchJobTitlesByParentId", condition);
            for (int i = 0; i < data.Rows.Count; i++)
            {
                BLJobTitle role = new BLJobTitle();
                role.Id = (Guid)data.Rows[i]["JobTitleId"];
                role.Name = (string)data.Rows[i]["JobTitleName"];
                role.Description = (string)data.Rows[i]["Description"];
                role.Avatar = (string)data.Rows[i]["Avatar"];
                role.DataSignature = (long)data.Rows[i]["DataSignature"];
                role.UpdateDate = (DateTime)data.Rows[i]["UpdateDate"];
                role.UpdateUserId = (Guid)data.Rows[i]["UpdateUserId"];
                role.Parent = new BLJobTitle();
                role.Parent.Id = (Guid)data.Rows[i]["ParentJobTitleId"];
                role.Parent.Name = string.Empty;
                result.Add(role);
            }
            return result;
        }

        public BLJobTitle BuildTree(List<BLJobTitle> list, Guid rootId)
        {
            BLJobTitle result = new BLJobTitle();
            result.Id = rootId;
            result.Name = ContentProvider.GetString("GeneralJobTitle");

            List<BLJobTitle> tempList = new List<BLJobTitle>();
            tempList.AddRange(list);

            while (tempList.Count > 0)
            {
                for (int i = 0; i < tempList.Count; i++)
                {
                    if (tempList[i].Id.Equals(result.Id))
                    {
                        result.Name = tempList[i].Name;
                        tempList.RemoveAt(i);
                        i--;
                    }
                    else
                    {
                        BLJobTitle parent = FindParent(result, tempList[i].Parent.Id);
                        if (parent != null)
                        {
                            tempList[i].Parent = parent;
                            parent.Children.Add(tempList[i]);
                            tempList.RemoveAt(i);
                            i--;
                        }
                        else
                        {
                            tempList.Add(tempList[i]);
                            tempList.RemoveAt(i);
                            i--;
                        }
                    }
                }
            }

            return result;
        }

        private BLJobTitle FindParent(BLJobTitle node, Guid parentId)
        {
            if (parentId.Equals(node.Id))
            {
                return node;
            }
            foreach (BLJobTitle child in node.Children)
            {
                BLJobTitle temp = FindParent(child, parentId);
                if (temp != null)
                {
                    return temp;
                }
            }
            return null;
        }

        public int AddJobTitle(BLJobTitle jobTitle, ref int errorCode)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            errorCode = 0;
            condition.Add(new BaseCondition("@id", jobTitle.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@name", jobTitle.Name, ParameterDirection.Input));
            condition.Add(new BaseCondition("@parentId", jobTitle.Parent.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@description", jobTitle.Description, ParameterDirection.Input));
            condition.Add(new BaseCondition("@avatar", jobTitle.Avatar, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", jobTitle.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_AddJobTitle", condition);
            errorCode = (int)condition[condition.Count-1].ParamValue;
            return result;
        }

        public int RenameJobTitle(BLJobTitle jobTitle, ref int errorCode)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            errorCode = 0;
            condition.Add(new BaseCondition("@id", jobTitle.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@name", jobTitle.Name, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", jobTitle.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_RenameJobTitle", condition);
            errorCode = (int)condition[condition.Count-1].ParamValue;
            return result;
        }

        public int MoveJobTitle(BLJobTitle jobTitle, ref int errorCode)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            errorCode = 0;
            condition.Add(new BaseCondition("@id", jobTitle.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@parentId", jobTitle.Parent.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", jobTitle.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_MoveJobTitle", condition);
            errorCode = (int)condition[condition.Count-1].ParamValue;
            return result;
        }

        public int DeleteJobTitle(BLJobTitle jobTitle, ref int errorCode)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            errorCode = 0;
            condition.Add(new BaseCondition("@id", jobTitle.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", jobTitle.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_DeleteJobTitle", condition);
            errorCode = (int)condition[condition.Count-1].ParamValue;
            return result;
        }
    }
}
